package teaching.api;

import org.springframework.web.bind.annotation.*;
import teaching.pojo.Homework;
import teaching.pojo.Submission;
import teaching.service.HomeworkService;
import teaching.util.Response;
import teaching.util.ResponseResult;

import javax.annotation.Resource;
import java.util.List;

/**
 * Author:20201002983_郑雨蓓
 * Date: 2024/3/19
 * Description:http://localhost:8080/homework
 */
@RestController
@RequestMapping("/homework")
public class HomeworkApi {
    @Resource
    private HomeworkService homeworkService;

    @GetMapping("/{cid}")
    public ResponseResult<List<Homework>> getCourse(@PathVariable("cid") Long cid) {
        List<Homework> homeworkList = homeworkService.listByCid(cid);
        return Response.createOkResp(homeworkList);
    }

    @PostMapping("")
    public ResponseResult<Object> createHomework(@RequestBody Homework newHomework) {
        System.out.println(newHomework);
        int res = homeworkService.addHomework(newHomework);
        return Response.createOkResp("添加成功");
    }

    @GetMapping("/{cid}/{sid}")
    public ResponseResult<Object> getStudentCourse(@PathVariable("cid") Long cid, @PathVariable("sid") Long sid) {
        List<Homework> homeworkList = homeworkService.listStudentHomework(cid, sid);
        return Response.createOkResp(homeworkList);
    }
    @PostMapping("/addSubmission")
    public ResponseResult<Object> addSubmission(@RequestBody Submission submission) {
        System.out.println(submission);
        int res = homeworkService.addSubmission(submission);
        return Response.createOkResp("添加成功");
    }
    @GetMapping("/submissions/{homeworkId}")
    public ResponseResult<List<Submission>> getHomeworkSubmissions(@PathVariable("homeworkId") long homeworkId) {
        List<Submission> submissions = homeworkService.getHomeworkSubmissions(homeworkId);
        return Response.createOkResp(submissions);
    }
    @PutMapping("/updateScore")
    public ResponseResult<Object> updateScore(@RequestBody Submission submission) {
        System.out.println("修改数据"+submission);
        int res = homeworkService.updateScore(submission);
        return Response.createOkResp("添加成功");
    }
}
